package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.Result;
import com.heima.domain.dto.VendingMachineDto;
import com.heima.domain.po.VendingMachine;
import com.heima.domain.po.VmType;
import com.heima.domain.vo.NodePageVo;
import com.heima.domain.vo.NodeVo;
import com.heima.domain.vo.RegionVo;
import com.heima.domain.vo.VmTypeVo;
import com.heima.mapper.RegionMapper;
import com.heima.mapper.VendingMachineMapper;
import com.heima.service.VendingMachineService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@AllArgsConstructor
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineMapper, VendingMachine> implements VendingMachineService {

    private VmTypeServiceImpl vmTypeServiceImpl;
    private NodeServiceImpl nodeServiceImpl;
    private RegionMapper regionMapper;

    /**
     * 设备分页查询
     *
     * @param pageIndex
     * @param pageSize
     * @param innerCode
     * @return
     */
    @Override
    public Result getByPage(Integer pageIndex, Integer pageSize, String innerCode) {
        Page<VendingMachine> page = new Page<>(pageIndex,pageSize);
        Page<VendingMachine> page1 = lambdaQuery().eq(StringUtils.hasText(innerCode), VendingMachine::getInnerCode, innerCode).page(page);
        List<NodeVo> list = BeanUtil.copyToList(page1.getRecords(), NodeVo.class);
        if(ObjectUtil.isEmpty(list)){
            return null;
        }
        for (NodeVo vo : list) {
            VmType byId = vmTypeServiceImpl.getById(vo.getVmType());
            VmTypeVo vmTypeVo = new VmTypeVo();
            BeanUtils.copyProperties(byId,vmTypeVo);
            vo.setType(vmTypeVo);

            Page<NodePageVo> nodePageVoPage = new Page<>(1,5);
            Page<NodePageVo> nodeByPage = nodeServiceImpl.getBaseMapper().getNodeByPage(nodePageVoPage, null, null, vo.getNodeId());
            if(nodeByPage.getRecords().size() > 0){
                NodePageVo nodePageVo = nodeByPage.getRecords().get(0);
                Page<RegionVo> page2 = new Page<>(1,1);
                Page<RegionVo> byPage = regionMapper.getByPage(page2, null, nodePageVo.getRegionId());
                if(byPage.getRecords().size() > 0){
                    nodePageVo.setRegion(byPage.getRecords().get(0));
                    vo.setNode(nodePageVo);
                    vo.setRegion(byPage.getRecords().get(0));
                }
            }
        }
        Result of = Result.of(page1);
        of.setCurrentPageRecords(list);
        return of;
    }

    /**
     * 新增设备
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean addVendingMachine(VendingMachineDto dto) {
        VendingMachine vendingMachine = new VendingMachine();
        BeanUtils.copyProperties(dto,vendingMachine);
        try {
            this.save(vendingMachine);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
